package ces.chenpt.autoconfig;

import ces.chenpt.converter.Converter;
import ces.chenpt.converter.SimpleConverter;
import ces.chenpt.interceptor.Interceptor;
import ces.chenpt.interceptor.ProxyChain;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by chenpt on 2019/10/12.
 */
@Configuration
@ConfigurationProperties(prefix = "ces.resources")
public class AutoConfiguration {
    private final Interceptor[] interceptors;
    private final ProxyChain<Converter> proxyChain = new ProxyChain<>();

    // 项目启动时，向“代理责任链”添加“Interceptor”实例
    public AutoConfiguration(ObjectProvider<Interceptor[]> provider) {
        this.interceptors = provider.getIfAvailable();
        for (Interceptor interceptor : interceptors) {
            proxyChain.addInterceptor(interceptor);
        }
    }

    // 加载所有代理对象，设置目标对象，创建目标对象的bean
    @Bean
    public Converter converter() {
        return proxyChain.all(new SimpleConverter());
    }

    private String staticLocation;

    public String getStaticLocation() {
        return staticLocation;
    }

    public void setStaticLocation(String staticLocation) {
        this.staticLocation = staticLocation;
    }
}
